var _table =null;
$(document).ready(function(){
	
	initSysRoleResourceTable();
	
	$("#sub_mitbtn").on("click",function(){
		
		var chk_value =[]; 
		$('#table-user input[type="checkbox"]:checked').each(function(){ 
			chk_value.push($(this).val()); 
		}); 
		
		if(chk_value.length == 0){
			layer.alert('请先选中菜单信息');
			return false;
		}
		
		var json_str = chk_value.join(",");
		//将选中的id,序列化保存
		ajaxRequestJson("saveOrUpdateSysRoleResource",{
			"idStr":json_str,
			"rid":$("#rid").val()
		},function(sh_result){
			if(sh_result>0){
				show_alert('保存成功',function(){layer_close()});
			}
		});
	})



});



function initSysRoleResourceTable(){
	
	 var $table = $('#table-user');
	    _table = $table.dataTable($.extend(true,{},CONSTANT.DATA_TABLES.DEFAULT_OPTION_NOPAG, {
		 orderMulti: false, //启用多列排序
		 bFilter: false,//去掉搜索框  
		 order: [], //取消默认排序查询,否则复选框一列会出现小箭头
		 renderer: "bootstrap", //渲染样式：Bootstrap和jquery-ui
		 pagingType: "simple_numbers", //分页样式：simple,simple_numbers,full,full_numbers
		 aaSorting: [[ 1, "desc" ]],//默认第几个排序
		 paging: false, // 禁止分页
		 bStateSave: true,//状态保存
		 language:"",
			pageLength:100,
			lengthMenu:[10,20,50,100],
			iDisplayLength: 10,
	        ajax : function(adata, callback, settings) {//ajax配置为function,手动调用异步查询
	            //封装请求参数
	           var param = "";
	            $.ajax({
	                    type: "post",
	                    url: "queryObjectListForRole",
	                    cache : false,  //禁用缓存
	                    data: param,    //传入已封装的参数
	                    dataType: "json",
	                    success: function(reslut) {
                            //封装返回数据，这里仅演示了修改属性名
                            var returnData = {};
                            returnData.draw = adata.draw;//这里直接自行返回了draw计数器,应该由后台返回
                            returnData.data = reslut;
                            callback(returnData);
	                    },
	                    error: function(XMLHttpRequest, textStatus, errorThrown) {
	                       console.log("查询失败");
	                    }
	                });
	        },
    "columns": [
				{
					 "data": "id","defaultContent": "","bSortable": false
				},
				{
					 "data": "name","defaultContent": "","bSortable": false
				},
				{
					 "data": "resourceType","defaultContent": "","bSortable": false
				}
     ],
     "columnDefs": [
    	 {
             "targets": [0],
             "data": "id",
             "render": function(data, type, full) { 
            	 return "";
             }
         },
         {
             "targets": [2], 
             "data": "resourceType", 
             "render": function(data, type, full) { 
            	 var str ="";
            	 if(data=='301'){
            		 str="目录";
            	 }else if(data=='302'){
            		 str="菜单";
            	 }
            	 return str;
             }
         },
         {
             "targets": [3], 
             "data": "id", 
             "render": function(data, type, full) { 
//            	 if(full.resourceType=="302" || full.resourceType=="301"){
            		 return '<input type="checkbox"  value='+data+'>';
//            	 }else{
//            		 return "";
//            	 }
             }
         }
     ],
	"rowCallback": function (row, data, displayIndex) {//行定义
              if (data.pid != "0") {
                  $(row).attr("class", "text-c treegrid-" + data.id + " treegrid-parent-" + data.pid);
             } else {
                 $(row).attr("class", "text-c treegrid-" + data.id);
             }
	},
	 "initComplete": function (settings, json) { //表格初始化完成后调用
		 _table =  $("#table-user").treegrid(
				 {
		                      "initialState": 'collapsed',
		                }
				 );
		 _table.treegrid('expandAll');
		 
		 //根据角色id，设置菜单被选中
		ajaxRequestJson("getSysResourceByRole",{
			"rid":$("#rid").val()
		},function(ret){
			if(ret != null && ret.length>0){
   				 for(var i=0;i<ret.length;i++){
   					 $('input:checkbox[value='+ret[i].resid+']').attr("checked", true);
   				 }
		     }
		});
		 
		 
		 
		 
		 
	},
	
	 })).api();   

}; 

